package com.shop.controller;

import com.shop.pojo.Goods;
import com.shop.result.JsonResult;
import com.shop.service.GoodsService;
import com.shop.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    ApplicationContext applicationContext;

    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Goods> selectAll(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<Goods> list = goodsService.selectAll();
        return list;
    }

    /*商品搜索*/
    @RequestMapping("/queryGoodsByCondition")
    @ResponseBody
    public JsonResult queryGoodsByCondition(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String data = request.getParameter("data");
        Map<String,Object> params = null;
        if(BaseUtil.isNotBlank(data)){
            data = BaseUtil.dataTranscoding(data);
            params = BaseUtil.json2Map(data);
        }else{
            params = new HashMap<>();
        }

        Map<String,Object> goodsList = goodsService.queryGoodsByCondition(params);
        return new JsonResult(true,goodsList.get("data"),null,Integer.parseInt(goodsList.get("total").toString()));
    }

    /*商品详细*/
    @RequestMapping("/getGoodsDetailByGoodsId")
    @ResponseBody
    public JsonResult getGoodsDetailByGoodsId(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Map<String,Object> goodsList = goodsService.getGoodsDetailByGoodsId(request,response);
        return new JsonResult(true,goodsList);
    }

    /*商品添加*/
    @RequestMapping("/addGood")
    @ResponseBody
    public JsonResult addGood(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String msg = goodsService.addGood(request,response);
        return new JsonResult(true,msg);
    }

    /*商品修改*/
    @RequestMapping("/editGood")
    @ResponseBody
    public JsonResult editGood(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String msg = goodsService.editGood(request,response);
        return new JsonResult(true,msg);
    }

    /*商品删除*/
    @RequestMapping("/deleteGood")
    @ResponseBody
    public JsonResult deleteGood(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String msg = goodsService.deleteGood(request,response);
        return new JsonResult(true,msg);
    }

    /*商品热销查询*/
    @RequestMapping("/getGoodsSaleByCondition")
    @ResponseBody
    public JsonResult getGoodsSaleByCondition(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<Goods> goodsList = goodsService.getGoodsSaleByCondition(request,response);
        return new JsonResult(true,goodsList);
    }

}
